package com.asking.service;

import com.asking.dao.CategoryMapper;
import com.asking.domain.Category;
import com.asking.domain.User;
import com.asking.vo.ResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 宋定鑫
 * @Date: 2018/10/17 15:42
 */
@Service
@Transactional
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    public  String updata(Category category) {
        List<Category> categoryList = categoryMapper.selectAll();
        for (Category cat : categoryList) {
            if(cat.getCname().equals(category.getCname())){
                return "分类已存在不能修改";
            }

        }
            categoryMapper.updateByPrimaryKey(category);
        return "分类修改成功";

    }

    public String add(Category category) {
        List<Category> categoryList = categoryMapper.selectAll();
        for (Category cat : categoryList) {
            if(cat.getCname().equals(category.getCname())){
                return "分类已添加";
            }


        }
        categoryMapper.insert(category);
        return "分类新增成功";
    }

    public Category selectBycategory(Integer id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        return category;

    }

    public ResultVo findAll(Integer page,Integer rows) {
        PageHelper.startPage(page,rows);// PageHelper 对紧跟着它的第一条sql语句进行分页操作
        List<Category> categoryList = categoryMapper.selectAll();
        PageInfo<Category> info = new PageInfo<>(categoryList);
        ResultVo resultVo = new ResultVo();
        resultVo.setTotal(info.getTotal());
        resultVo.setRows(info.getList());

        return resultVo;
    }

    public void deleteidcategory(Integer id) {
        categoryMapper.deleteByPrimaryKey(id);
    }
}
